package o;

import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build.VERSION;
import android.os.Environment;
import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;
import android.text.TextUtils;
import androidx.annotation.RequiresApi;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Stack;

public class bbw {
    static final String[] ˊ = new String[]{"/tencent/MicroMsg", "/Tencent/MicroMsg"};
    private static final String[] ॱ = new String[]{"/Huawei/Backup", "/HuaweiBackup", "/backup", "/alipay/multimedia/", "/tencent/MobileQQ/diskcache", "/tencent/MobileQQ/pddata", "/sina/weibo/small_page", "/immomo/mk/offline/", "/immomo/imgcache/"};

    static class d implements FileFilter {
        private String ॱ;

        public d(String str) {
            this.ॱ = str;
        }

        public boolean accept(File file) {
            return file.isDirectory() && !file.getPath().endsWith(this.ॱ);
        }
    }

    @RequiresApi(api = 24)
    public static boolean ˏ(Activity activity, Stack<String> stack, ArrayList<String> arrayList) {
        StorageManager storageManager = (StorageManager) activity.getSystemService("storage");
        ॱ(arrayList);
        ˎ(arrayList, "/mnt/asec");
        ˎ(arrayList, "/mnt/secure");
        if (VERSION.SDK_INT < 23) {
            ॱ(stack, storageManager, arrayList);
        } else if (cgt.ˋ(activity)) {
            return false;
        } else {
            if (ˏ(activity, 10001)) {
                try {
                    for (Object invoke : (StorageVolume[]) StorageManager.class.getMethod("getVolumeList", new Class[0]).invoke(storageManager, new Object[0])) {
                        String str = (String) StorageVolume.class.getMethod("getPath", new Class[0]).invoke(invoke, new Object[0]);
                        stack.add(str);
                        for (String str2 : ॱ) {
                            ˎ(arrayList, str + str2);
                        }
                        for (String str22 : ˊ) {
                            ˏ((ArrayList) arrayList, str, str22);
                        }
                    }
                } catch (NoSuchMethodException e) {
                    bbn.ˎ.ˊ("ApkScanDirUtils", "can not find method" + e.toString());
                } catch (IllegalAccessException e2) {
                    bbn.ˎ.ˊ("ApkScanDirUtils", "can not find method" + e2.toString());
                } catch (InvocationTargetException e3) {
                    bbn.ˎ.ˊ("ApkScanDirUtils", "can not find method" + e3.toString());
                }
            } else {
                bbn.ˎ.ˏ("ApkScanDirUtils", "no permission");
                return false;
            }
        }
        ˋ(activity, stack);
        return true;
    }

    private static void ˏ(ArrayList<String> arrayList, String str, String str2) {
        if (arrayList != null && !TextUtils.isEmpty(str) && !TextUtils.isEmpty(str2)) {
            File[] listFiles = new File(str + str2).listFiles(new d(str2 + "/Download"));
            if (listFiles == null || listFiles.length == 0) {
                bbn.ˎ.ˊ("ApkScanDirUtils", "addWeChatFileToWhiteList: the weChatFiles is null or length is 0");
                return;
            }
            for (File file : listFiles) {
                if (file != null) {
                    ˎ(arrayList, file.getAbsolutePath());
                }
            }
        }
    }

    private static void ॱ(ArrayList<String> arrayList) {
        Object obj = null;
        if (arrayList == null) {
            bbn.ˎ.ˊ("ApkScanDirUtils", "addDownloadFileToWhiteList; whiteList is null.");
            return;
        }
        Object canonicalPath;
        cfl ˎ = cfm.ˎ();
        if (ˎ != null) {
            try {
                canonicalPath = new File(ˎ.ˎ(), "tmp").getCanonicalPath();
            } catch (IOException e) {
                bbn.ˎ.ˊ("ApkScanDirUtils", "tmp file error.");
                canonicalPath = obj;
            }
            try {
                obj = new File(ˎ.ˎ(), "SkinResource").getCanonicalPath();
            } catch (IOException e2) {
                bbn.ˎ.ˊ("ApkScanDirUtils", "skin resource file error.");
            }
        } else {
            canonicalPath = obj;
        }
        if (canonicalPath != null) {
            arrayList.add(canonicalPath);
        }
        if (obj != null) {
            arrayList.add(obj);
        }
    }

    private static void ˋ(Activity activity, Stack<String> stack) {
        try {
            File cacheDir = activity.getCacheDir();
            stack.push(cacheDir == null ? "" : cacheDir.getCanonicalPath());
        } catch (IOException e) {
            bbn.ˎ.ˋ("ApkScanDirUtils", "can not get cache dir.");
        }
    }

    private static void ˎ(ArrayList<String> arrayList, String str) {
        try {
            arrayList.add(new File(str).getCanonicalPath());
        } catch (IOException e) {
            bbn.ˎ.ˊ("ApkScanDirUtils", "error path:" + str);
        }
    }

    @TargetApi(23)
    public static boolean ˏ(Activity activity, int i) {
        int ˋ = cdv.ˋ();
        if ((ˋ >= 9 && ˋ < 17 && activity.checkSelfPermission("android.permission.WRITE_MEDIA_STORAGE") == 0) || -1 != activity.checkSelfPermission("android.permission.WRITE_EXTERNAL_STORAGE")) {
            return true;
        }
        try {
            activity.requestPermissions(new String[]{"android.permission.WRITE_EXTERNAL_STORAGE"}, i);
            return false;
        } catch (Exception e) {
            bbn.ˎ.ˊ("ApkScanDirUtils", "checkPermission: request permissions exception!");
            return false;
        }
    }

    private static void ॱ(Stack<String> stack, StorageManager storageManager, ArrayList<String> arrayList) {
        int i;
        Object obj = null;
        int i2 = 0;
        String str = "/HWUserData";
        stack.push(str);
        for (String str2 : ॱ) {
            ˎ(arrayList, str + str2);
        }
        try {
            obj = StorageManager.class.getMethod("getVolumePaths", new Class[0]).invoke(storageManager, new Object[0]);
        } catch (NoSuchMethodException e) {
            bbn.ˎ.ˋ("ApkScanDirUtils", "invoke failed, NoSuchMethodException");
        } catch (IllegalAccessException e2) {
            bbn.ˎ.ˋ("ApkScanDirUtils", "invoke failed, IllegalAccessException");
        } catch (IllegalArgumentException e3) {
            bbn.ˎ.ˋ("ApkScanDirUtils", "invoke failed, IllegalArgumentException");
        } catch (InvocationTargetException e4) {
            bbn.ˎ.ˋ("ApkScanDirUtils", "invoke failed, InvocationTargetException");
        }
        int length;
        if (obj == null || !(obj instanceof String[])) {
            try {
                String canonicalPath = Environment.getExternalStorageDirectory().getCanonicalPath();
                stack.push(canonicalPath);
                String[] strArr = ॱ;
                length = strArr.length;
                while (i2 < length) {
                    ˎ(arrayList, canonicalPath + strArr[i2]);
                    i2++;
                }
                return;
            } catch (IOException e5) {
                bbn.ˎ.ˊ("ApkScanDirUtils", "can not getCanonicalPath");
                return;
            }
        }
        String[] strArr2 = (String[]) obj;
        for (i = 0; i < strArr2.length; i++) {
            stack.push(strArr2[i]);
            for (String str22 : ॱ) {
                ˎ(arrayList, strArr2[i] + str22);
            }
        }
    }
}
